package site.wetsion.employ.server.util;

import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
 * 发送验证码邮件的工具类
 * @author Wetsion
 *
 */
public class SendCode {

	public static void sendCodeByEmail(String code,String target) throws AddressException, MessagingException{
		Properties props = new Properties();  
        // 开启debug调试  
        props.setProperty("mail.debug", "true");  
        // 发送服务器需要身份验证  
        props.setProperty("mail.smtp.auth", "true");  
        // 设置邮件服务器主机名  
        props.setProperty("mail.host", "smtp.163.com");  
        // 发送邮件协议名称  
        props.setProperty("mail.transport.protocol", "smtp");  
          
        // 设置环境信息  
        Session session = Session.getInstance(props);  
          
        // 创建邮件对象  
        Message msg = new MimeMessage(session);  
        msg.setSubject("欢迎注册城建就业指导网");  
        // 设置邮件内容  
        msg.setText("您的指导网注册验证码为："+code);  
        // 设置发件人  
        msg.setFrom(new InternetAddress("m13110018525@163.com"));  
          
        Transport transport = session.getTransport();  
        // 连接邮件服务器  
        transport.connect("m13110018525", "weixin1307070205");  
        // 发送邮件  
        transport.sendMessage(msg, new Address[] {new InternetAddress(target)});  
        // 关闭连接  
        transport.close();  
	}
	
	
	public static void sendEmail(String title, String content,String target) throws AddressException, MessagingException{
		Properties props = new Properties();  
        // 开启debug调试  
        props.setProperty("mail.debug", "true");  
        // 发送服务器需要身份验证  
        props.setProperty("mail.smtp.auth", "true");  
        // 设置邮件服务器主机名  
        props.setProperty("mail.host", "smtp.163.com");  
        // 发送邮件协议名称  
        props.setProperty("mail.transport.protocol", "smtp");  
          
        // 设置环境信息  
        Session session = Session.getInstance(props);  
          
        // 创建邮件对象  
        Message msg = new MimeMessage(session);  
        msg.setSubject(title);  
        // 设置邮件内容  
        msg.setText(content);  
        // 设置发件人  
        msg.setFrom(new InternetAddress("m13110018525@163.com"));  
          
        Transport transport = session.getTransport();  
        // 连接邮件服务器  
        transport.connect("m13110018525", "weixin1307070205");  
        // 发送邮件  
        transport.sendMessage(msg, new Address[] {new InternetAddress(target)});  
        // 关闭连接  
        transport.close();  
	}
	
	public static void main(String[] args) {
		try {
			SendCode.sendCodeByEmail("mjds", "2399269881@qq.com");
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

